package util;

import com.alibaba.fastjson.JSON;
import org.junit.Test;

import java.util.*;
import java.util.logging.Logger;

public class CollectionsTest {

    private static final Logger logger = Logger.getLogger(String.valueOf(CollectionsTest.class));

    /**
     * sort() 排序
     */
    @Test
    public void collectionsSort() {
        List<Integer> list = Arrays.asList(1, 5, 2, 3, 4, 11, 123);
        Collections.sort(list);
        logger.info(Objects.toString(list));
    }

    /**
     * Collections 内置的空集合
     */
    @Test
    public void collectionsEmpty() {
        List emptyList = Collections.EMPTY_LIST;
        Map emptyMap = Collections.EMPTY_MAP;
        Set emptySet = Collections.EMPTY_SET;
        logger.info(emptyList.toString());
        logger.info(emptyMap.toString());
        logger.info(emptySet.toString());
    }

    /**
     * binarySearch()
     */
    @Test
    public void collectionsBinarySearch() {
        List<Integer> list = Arrays.asList(1, 5, 2, 3, 4, 11, 123);
        int i = Collections.binarySearch(list, 17);
        logger.info(String.valueOf(i));
    }

    /**
     * max() 最大值
     * min() 最小值
     */
    @Test
    public void collectionsMaxMin() {
        List<Integer> list1 = Arrays.asList(1, 5, 2, 3, 4, 11, 123);
        Integer max = Collections.max(list1);
        Integer min = Collections.min(list1);
        logger.info("最大值：" + max);
        logger.info("最小值：" + min);
    }

    /**
     * Collections 中不可变的 类
     */
    @Test
    public void testOne() {
        Map<String, String> map = new HashMap<>();
        map.put("one", "one");
        map.put("two", "one");
        map.put("three", "one");
        Map<String, String> mp = Collections.unmodifiableMap(map);
        mp.put("one", "two");
    }

    /**
     * Collections 中不可变的 类
     */
    @Test
    public void testTwo() {
        List<String> list = new ArrayList<>();
        list.add("张三");
        list.add("张三");
        list.add("李四");
        List<String> unmodifiableList = Collections.unmodifiableList(list);
        unmodifiableList.add("test");
        logger.info(JSON.toJSONString(list));
    }


}
